const { Claim } = require('../models/claimModel');

//  获取理赔数据
async function getClaim(req, res, next) {
      const { type = "name", value = "", pageSize = 5, currentPage = 2 } = req.query;

      const total = await Claim.countDocuments();

      const totalPages = Math.ceil(total / pageSize);
      const data = await Claim
            .find({ [type]: { $regex: value, $options: '$i' } })
            .limit(pageSize - 0)
            .skip((currentPage - 1) * pageSize)
            .populate('orderId', 'productId', 'customerId')

      res.send({
            status: 1,
            data: { total, totalPages, data, pageSize, currentPage },
            message: '学生数据获取成功'
      })

}

//  新增理赔数据
async function addClaim(req, res, next ) {

}

//  修改理赔数据
async function updateClaim(req, res, next) {
      try {
            const { _id, name, age, gender, tel, img} = req.body;
            const data = await Claim.updateOne({_id}, { name, age, gender, tel, img});
            if (data.nModified === 1) {
                  res.send({
                      status: 1,
                      message: '修改成功'
                  })
              } else {
                  res.send({
                      status: 0,
                      message: '修改失败'
                  })
              } 
      } catch (error) {
            console.log('修改失败', error);
            res.send({
                  status: 0,
                  message: '修改失败'
            })
      }
      
}

//  删除理赔数据
async function deleteClaim(req, res, next ){
      const { _id } = req.body;
      try {
            const data = await Claim.findByIdAndDelete(_id);
            if (data) {
                  res.send({
                        status: 1,
                        message: '删除成功'
                  })
            } else {
                  res.send({
                        status: 0,
                        message: '删除失败'
                  })
            }
      } catch (error) {
            console.log('删除失败' , error);
            res.send({
                  status: 0,
                  message: '删除失败'
            })
      }

}

module.exports = { getClaim, addClaim, updateClaim, deleteClaim }